package com.baselibrary.utils

import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.text.TextUtils
import android.widget.TextView
import androidx.databinding.adapters.TextViewBindingAdapter.setText


object TextColorGradientUtils {
    /**
     * 文本渐变 尊享
     */
    @JvmStatic
    fun setGradient(textView: TextView) {
        val endX = textView.paint.textSize * textView.text.length
        val colors = intArrayOf(
                Color.parseColor("#FFD0B487"),
                Color.parseColor("#FFF6EEC3"),
                Color.parseColor("#FFD0B487")
        ) //颜色的数组
        val position = floatArrayOf(0.2f, 0.5f, 0.8f) //颜色渐变位置的数组
        val linearGradient =
                LinearGradient(0f, 0f, endX, 0f, colors, position, Shader.TileMode.MIRROR)
        textView.paint.shader = linearGradient
        textView.invalidate()
    }

    /**
     * 文本渐变 0元购
     */
    @JvmStatic
    fun setGradient4ZeroBuy(textView: TextView) {
        val endX = textView.paint.textSize * textView.text.length
        val colors = intArrayOf(
                Color.parseColor("#CE47F2"),
                Color.parseColor("#6F0CF8")
        ) //颜色的数组
        val position = floatArrayOf(0.2f, 0.8f) //颜色渐变位置的数组
        val linearGradient = LinearGradient(0f, 0f, endX, 0f, colors, position, Shader.TileMode.MIRROR)
        textView.paint.shader = linearGradient
        textView.invalidate()
    }

    /**手机号星号
     * @author: merbng
     * @time: 2021/11/10 17:49
     * @description:
     */
    @JvmStatic
    fun phoneStarFormat(pNumber: String): String {
        val sb = StringBuilder()
        if (!TextUtils.isEmpty(pNumber) && pNumber.length > 6) {
            for (i in 0 until pNumber.length) {
                val c = pNumber.get(i)
                if (i >= 3 && i <= 6) {
                    sb.append('*')
                } else {
                    sb.append(c)
                }
            }
        }
        return sb.toString()
    }
}
